package team.lourd.network.system.batch.ext.support;

import lombok.Data;
import team.lourd.network.system.batch.domian.ProcessCondition;
import team.lourd.network.system.batch.ext.ConditionAnalyser;
import team.lourd.network.system.batch.ext.ConditionExecutor;
import team.lourd.network.system.batch.ext.ConditionNode;
import team.lourd.network.system.batch.logic.ConditionType;
import team.lourd.network.system.batch.logic.LogicCharacter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 逻辑标记条件处理器
 *
 * @author JADemo
 * @date 2019/2/18 14:29
 */
@Data
public class LogicConditionExecutor implements ConditionExecutor {
    private ConditionAnalyser analyser;

    // 当前Condition是否为Simple类型
    @Override
    public boolean support(ProcessCondition processCondition) {
        return processCondition.getType() == ConditionType.SIMPLE;
    }

    // 处理条件组
    @Override
    public boolean execute(ProcessCondition processCondition) {
        // 使用条件分析器,解刨条件
        List<ConditionNode> conditionNodes = analyser.analyse(processCondition);
        for (ConditionNode conditionNode : conditionNodes) {
            // 获取条件节点体
            String nodeBody = conditionNode.getContent();
            // 中间结果
            boolean intermediate = true;
            // 如果子节点里面包含And条件就进行拆分
            if (nodeBody.contains(LogicCharacter.And.name())) {
                // 根据And来拆分条件
                String[] subNodeBodyArray = nodeBody.split(LogicCharacter.And.name());
                // 拆分处理每一个子节点的Body
                for (String subNodeBody : subNodeBodyArray) {
                    // 处理获取结果
                    intermediate = doExecute(subNodeBody);
                    // 如果为false直接跳过
                    if (!intermediate) {
                        break;
                    }
                }
            } else {
                // 处理获取结果
                intermediate = doExecute(nodeBody);
            }
            conditionNode.setSuccess(intermediate);
            if (intermediate) {
                return intermediate;
            }
        }
        return false;
    }

    // 执行对子条件的判断
    protected boolean doExecute(String body) {
        // 去空格
        body = body.replace(" ", "");
        // 最终返回结果
        boolean success;
        if (body.contains("<")) {
            // 使用当前逻辑符号分割数字
            List<Long> numbers = Arrays.asList(body.split("<")).stream().map((str) -> Long.valueOf(str)).collect(Collectors.toList());
            if (numbers.size() != 2) {
                throw new RuntimeException("错误的条件格式");
            }
            // 执行判断
            success = numbers.get(0) < numbers.get(1);
        } else if (body.contains(">")) {
            List<Long> numbers = Arrays.asList(body.split(">")).stream().map((str) -> Long.valueOf(str)).collect(Collectors.toList());
            if (numbers.size() != 2) {
                throw new RuntimeException("错误的条件格式");
            }
            success = numbers.get(0) > numbers.get(1);
        } else if (body.contains("=")) {
            try {
                List<Long> numbers = Arrays.asList(body.split("=")).stream().map((str) -> Long.valueOf(str)).collect(Collectors.toList());
                if (numbers.size() != 2) {
                    throw new RuntimeException("错误的条件格式");
                }
                success = numbers.get(0) == numbers.get(1);
                // 发生转换异常的时候使用字符串判断
            } catch (NumberFormatException e) {
                List<String> strs = Arrays.asList(body.split("="));
                if (strs.size() != 2) {
                    throw new RuntimeException("错误的条件格式");
                }
                success = strs.get(0).equals(strs.get(1));
            }
        } else {
            throw new RuntimeException("无法识别的逻辑符");
        }
        return success;
    }

}
